#include <iostream>

void incr(int val) {
	val++;
}
void incr2(int& val) {
	val++;
}
void incr3(int* ptr) {
	(*ptr)++;
}

/**
 * 默认值传递
 * 引用传递和指针传递可以直接修改外面的变量
 */
void Test01(){
    int a = 8;
    incr(a);
    std::cout << "值传递" << std::endl;
    std::cout << "a=" << a << std::endl;
    std::cout << "===============\n";

    a = 8;
    incr2(a);
    std::cout << "引用传递" << std::endl;
    std::cout << "a=" << a << std::endl;
    std::cout << "===============\n";


    a = 8;
    incr3(&a);
    std::cout << "指针作为函数参数" << std::endl;
    std::cout << "a=" << a << std::endl;

}


/*
引用是无法改变的，int& ref = a;后，ref就和a绑定了
ref=b并不是将ref和b进行绑定，而是将b的值赋给了ref，等价于ref=5
-------------------------
指针是可以改变指向的，第一次可以获取a的地址，第二次可以获取b的地址
*/
void Test02(){
	int a = 8;
	int b = 5;

	int& ref = a;
	std::cout << "==========ref指向a后==========" << std::endl;
	std::cout << "a=" << a << std::endl;
	std::cout << "b=" << b << std::endl; 
	std::cout << "ref=" << ref << std::endl;

	ref = b;
	std::cout << "==========ref再次指向b后==========" << std::endl;
	std::cout << "a=" << a << std::endl;
	std::cout << "b=" << b << std::endl;
	std::cout << "ref=" << ref << std::endl;

	ref = 11;
	std::cout << "==========修改ref的值==========" << std::endl;
	std::cout << "a=" << a << std::endl;
	std::cout << "b=" << b << std::endl;
	std::cout << "ref=" << ref << std::endl;

	int* ptrA = &a;
	*ptrA = 9;
	std::cout << "==========通过指针修改a的内存==========" << std::endl;
	std::cout << "a=" << a << std::endl;
	
	ptrA = &b;
	*ptrA = 100;
	std::cout << "==========通过指针修改b的内存==========" << std::endl;
	std::cout << "a=" << a << std::endl;
	std::cout << "b=" << b << std::endl;
}

//指针和引用混合用
void Test03(){
	int a = 8;
	int* ptr = &a;

    //这和int& ref = a;是等价的
	int& ref = *ptr;

    ref = 777;
    std::cout << "a=" << a << std::endl;
}


/*
指针与引用的区别
*/
int main(){

    // Test01();

    // Test02();

    Test03();

    return 0;
}